package com.toe.commodity.webapi.controller;


import com.toe.commodity.webapi.service.impl.CommodityGoodsAlbumServiceImpl;
import com.toe.commodity.webapi.service.impl.CommodityPictureServiceImpl;
import com.toe.common.restful.JsonResult;
import com.toe.pojo.vo.commodity.CommodityGoodsAlbumVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/webapi/commodity-goods-album")
public class CommodityGoodsAlbumController {

    @Autowired
    CommodityGoodsAlbumServiceImpl commodityGoodsAlbumService;

    @Autowired
    CommodityPictureServiceImpl commodityPictureService;

    @GetMapping("/getGoodsUrlByGoodsId/{id}")
    @ApiOperation("根据商品id获取商品相册")
    public JsonResult getGoodsUrlByGoodsId(@PathVariable("id") Long id){
        List<CommodityGoodsAlbumVO> goodsUrlByGoodsId = commodityGoodsAlbumService.getGoodsUrlByGoodsId(id);
        return JsonResult.ok("获取成功",goodsUrlByGoodsId);
    }

    @GetMapping("/addNewGoodsUrl")
    @ApiOperation("插入新的商品相册图片")
    public JsonResult addNewGoodsUrl( CommodityGoodsAlbumVO commodityGoodsAlbumVO, @RequestParam("file") MultipartFile mpfile){
        String path = commodityPictureService.pictureUpload(mpfile);
        commodityGoodsAlbumVO.setUrl(path);
        commodityGoodsAlbumService.addNewGoodsUrl(commodityGoodsAlbumVO);
        return JsonResult.ok("图片新增完成");
    }

    @GetMapping("/deletedAlbumByUrl/{id}")
    @ApiOperation("根据图片id删除商品相册中对应的图片")
    public JsonResult deletedAlbumByUrl(@PathVariable("id") Long id){
        commodityGoodsAlbumService.deletedAlbumByUrl(id);
        return JsonResult.ok("删除成功");
    }



}

